/*
	Author      : Sidek Sirun
	Course      : UCI Java Programming I (X460.10)
	Assignment 7: Java Libraries

	Your first program should expand your program submitted last 
	week by first counting all people with the same last name and 
	then changing the last names to Smith. 
	(using Apache Commons library)
	Your second program should provide the exact same functionality 
	described but without making use of the Apache Commons library.

	This is the second program without using Apache Common Library.
*/

// Java standard libraries
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class LastNameUtils2 {

	/**
	* @lastNameCompare() method
	*/
	public static class lastNameCompare implements Comparator<Person> {
		public int compare(Person p1, Person p2) {
			return p1.getLastName().compareTo(p2.getLastName());
		}
	}

	/**
	* @displayCollection()
	*/
	public static void displayCollection(ArrayList<Person> personObjectList) {
		ArrayList<Person> pList = personObjectList;
		for (int j=0; j < pList.size(); j++) {
			//System.out.println(personObjectList.get(j).toString());
			String pFName  = pList.get(j).getFirstName();
			String pLName  = pList.get(j).getLastName();
			int    pAge    = pList.get(j).getAge();
			String pGender = pList.get(j).getGender();
			String pSSN    = pList.get(j).getSSN();
			System.out.println("Last Name: " + pLName + ", First Name: " + pFName + ", Age: " + pAge + ", Gender: " + pGender + ", SSN: " + pSSN);
		}
	}
	/**
	* @findSameLastname() method
	*/
	public static ArrayList<String> findSameLastName(ArrayList<Person> personObjectList) {
		System.out.println("=== findSameLastName() ===");
		ArrayList<Person> pList         = personObjectList;
		ArrayList<String> lNameList     = new ArrayList<String>();
		ArrayList<String> duplicateList = new ArrayList<String>();
		String cName                    = "";
		int    countLName               = 0 ;

		for (int j=0; j < pList.size(); j++) {
			String lName = pList.get(j).getLastName();
			lNameList.add(lName);
		}
		//debug//System.out.println("List of Last Name: " + lNameList.toString());

		for (String lName : lNameList) {
			countLName = Collections.frequency(lNameList, lName);
			//debug//System.out.println("Last name: " + lName + ", Count: " + countLName);
			if (countLName > 1) {
				duplicateList.add(lName);
			}
		}

		for (String name : duplicateList) {
			int count = Collections.frequency(duplicateList, name);
			if (!name.equals(cName)) {
				System.out.println(count + " persons have " + name + " as last name.");
			}
			cName = name;
		}

		//debug//System.out.println(duplicateList);
		return duplicateList;
	}

	/**
	* @updateLastName()
	*/
	public static ArrayList<Person> updateLastName(ArrayList<Person> personObjectList, ArrayList<String> duplicateList, String newLastName) {
		System.out.println("=== updateLastName() ===");
		ArrayList<Person> pList = personObjectList;
		ArrayList<String> dList = duplicateList;
		String nLastName        = newLastName;

		for (String lName : dList) {
			for (int y=0; y < pList.size(); y++) {
				String plastName  = pList.get(y).getLastName();
				String pfirstName = pList.get(y).getFirstName();
				if (plastName == lName) {
					System.out.println("Updating last name \"" + lName + ", " + pfirstName + "\" to \"" + newLastName + ", " + pfirstName + "\".");
					pList.get(y).setLastName(newLastName);
				}
			}
		}

		// New Updated Person List
		return pList;
	}

	/**
	* @main() method
	*/
	public static void main(String[] args) {
	
		String   TITLE    = "===== LastNameUtils2 Class Test =====";
		File     personFileHandler;
		Scanner  fileScanner;
		String   rdata;
		String[] dataList = new String[] {};

		// Person collection
		ArrayList<Person>  personObjectList = new ArrayList<Person>();
		// Person attributes
		String  firstName = "";
		String  lastName  = "";
		int     age       = 0 ;
		String  gender    = "";
		String  ssn       = "";

		// Array list for same last name list
		ArrayList<String> duplicateList = new ArrayList<String>();
		// Array list for updated last name list
		ArrayList<Person> newPersonList = new ArrayList<Person>();

		try {
			personFileHandler  = new File("personData.txt");
			fileScanner        = new Scanner(personFileHandler);
			//fileScanner.useDelimiter(",");

			while (fileScanner.hasNextLine()) {
				rdata    = fileScanner.nextLine();
				dataList = rdata.split(",");

				// Populate person infomation from file
				for (int i=0; i < dataList.length; i++) {
					firstName = dataList[0];
					lastName  = dataList[1];
					age       = Integer.parseInt(dataList[2]);
					gender    = dataList[3];
					ssn       = dataList[4];
				}

				// Person object
				Person p = new Person(firstName, lastName, age, gender, ssn);
				// add person object to personObjectList
				personObjectList.add(p);
			}

			fileScanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		// Print title
		System.out.println("\n" + TITLE);

		// Print the personObjectList content to STDOUT
		//System.out.println("");
		//System.out.println("Person data before sorting by last name");
		System.out.println("Person data before changing last name");
		System.out.println("-------------------------------------");
		displayCollection(personObjectList);

		// Sort person object list by last name
		//Collections.sort(personObjectList, new lastNameCompare());
		
		//System.out.println("");
		//System.out.println("Person data after sorting by last name");
		//System.out.println("--------------------------------------");
		//displayCollection(personObjectList);
		
		System.out.println("");

		// Find same last name
		duplicateList = findSameLastName(personObjectList);

		System.out.println("");
		
		// Update last name
		newPersonList = updateLastName(personObjectList, duplicateList, "Smith");
		System.out.println("");
		System.out.println("Person data after changing last name");
		System.out.println("------------------------------------");
		displayCollection(newPersonList);

		System.out.println("");

	}
}
